﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using 刘颜红销售项目管理.Business.DOM ;
using XDesigner.ORM;
using 刘颜红销售项目管理.Business;

namespace 刘颜红销售项目管理
{
    public partial class frm费用信息 : DevExpress.XtraEditors.XtraForm
    {
        public frm费用信息()
        {
            InitializeComponent();
            strTitle = this.Text;
            //this.repositoryItemPopupContainerEdit1.PopupControl = this.peopleListControl;

        }
        
        private string strTitle = null;
        
        private void btnRefresh_Click(object sender, EventArgs e)
        {
            MyApplicationFramework.Instance.ORMEngine.OpenConnection();
            tvwPeoples.Nodes.Clear();
            foreach (Class工作单位 unit in MyApplicationFramework.Instance.ORMEngine.ReadAllInstances(typeof(Class工作单位)))
            {
                TreeNode unitNode = new TreeNode(unit.单位名称);
                tvwPeoples.Nodes.Add(unitNode);
                foreach (Class人员基本信息 people in unit.人员列表)
                {
                    TreeNode peopleNode = new TreeNode(people.姓名);
                    peopleNode.Tag = people;
                    unitNode.Nodes.Add(peopleNode);
                }
            }

            //Array units = MyApplicationFramework.Instance.ORMEngine.ReadAllInstances(typeof(Class工作单位));
            //foreach (Class工作单位 unit in units)
            //{
            //    TreeNode unitNode = new TreeNode(unit.单位名称);
            //    tvwList.Nodes.Add(unitNode);
            //    unitNode.Tag = unit;
            //    foreach (Class人员基本信息 people in unit.人员列表)
            //    {
            //        TreeNode peopleNode = new TreeNode(people.姓名);
            //        peopleNode.Tag = people;
            //        unitNode.Nodes.Add(peopleNode);
            //    }
            //}

            ORMBindingSource ds = MyApplicationFramework.Instance.ORMEngine.CreateBindingSource(
                typeof( Class费用信息 ),
                MyApplicationFramework.Instance.ORMEngine.ReadAllInstances( typeof( Class费用信息 )));

            ds.LogItemChange = true;
            ds.Changed += new EventHandler(ds_Changed);
            myGridControl.DataSource = ds;
            MyApplicationFramework.Instance.ORMEngine.CloseConnection();
        }

        void ds_Changed(object sender, EventArgs e)
        {
            this.Text = strTitle + "*";
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            DevExpress.XtraEditors.INavigatableControl ctl = (DevExpress.XtraEditors.INavigatableControl)myGridControl;
            ctl.DoAction(DevExpress.XtraEditors.NavigatorButtonType.Append);
            //DataGridViewRow row = myGridControl myDataGridView.Rows[ myDataGridView.NewRowIndex ] ;
            //row.Selected = true ;
            //myDataGridView.CurrentCell = row.Cells[1];
            //myDataGridView.BeginEdit(false );
        }

        private void frm单位基本信息_Load(object sender, EventArgs e)
        {
            chkStartDate.Checked = true;
            dtpStartDate.Value = DateTime.Today.AddDays(-10);

            chkEndDate.Checked = false;
            dtpEndDate.Value = DateTime.Today;

            btnSearch_Click(null, null);
            //btnRefresh_Click(null, null);
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            DevExpress.XtraEditors.INavigatableControl ctl = (DevExpress.XtraEditors.INavigatableControl)myGridControl;
            ctl.DoAction(DevExpress.XtraEditors.NavigatorButtonType.Remove);
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            ORMBindingSource ds = (ORMBindingSource)myGridControl.DataSource;
            if (ds.Modified)
            {
                if (MessageBox.Show(
                    this,
                    "记录已经修改，是否保存?",
                    "系统提示",
                    MessageBoxButtons.YesNo,
                    MessageBoxIcon.Question)
                    == DialogResult.Yes)
                {
                    btnSave_Click(null, null);
                }
            }
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            ORMBindingSource ds = (ORMBindingSource) this.myGridControl.DataSource;
            if (ds.Modified)
            {
                ds.Engine.OpenConnection();
                int result = ds.Update();
                ds.Engine.CloseConnection();
                ds.Engine.Buffer.Clear(typeof(Class费用信息));
                this.Text = strTitle;
                MessageBox.Show("更新了 " + result + " 条记录.");
            }
        }
          

        private void tvwList_AfterSelect(object sender, TreeViewEventArgs e)
        {
            //if (tvwList.SelectedNode == null)
            //    return;
            //if (tvwList.SelectedNode.Tag is Class工作单位)
            //{
            //    Class工作单位 unit = (Class工作单位)tvwList.SelectedNode.Tag;
            //    Class费用信息 condition = new Class费用信息();
            //    condition.参与人员 
            //}
        }

        private void peopleListControl_Popup(object sender, EventArgs e)
        {

        }

        private void btnOKPeopleList_Click(object sender, EventArgs e)
        {
            if (this.popupContainerControl.OwnerEdit != null)
            {
                this.popupContainerControl.OwnerEdit.ClosePopup();
            }
        }

        private void btnCancelPeople_Click(object sender, EventArgs e)
        {
            if (this.popupContainerControl.OwnerEdit != null)
            {
                this.popupContainerControl.OwnerEdit.CancelPopup();
            }
        }

        private void editorPeoples_QueryPopUp(object sender, CancelEventArgs e)
        {
             
        }

        private void editorPeoples_QueryResultValue(object sender, DevExpress.XtraEditors.Controls.QueryResultValueEventArgs e)
        {
             
        }

        private void editorPeoples_QueryDisplayText(object sender, DevExpress.XtraEditors.Controls.QueryDisplayTextEventArgs e)
        {
             
        }

        private void btnShowSearch_Click(object sender, EventArgs e)
        {
            pnlSearch.Visible = btnShowSearch.Checked;
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            QueryConditionList conditions = new QueryConditionList();
            Class人员基本信息 people = ( Class人员基本信息 ) txtPeople.EditValue ;
            if (people != null)
            {
                conditions.AddCondition(" 费用信息表.RecordID in ( Select 费用编号 From 费用关系表 Where 人员编号 = " + people.RecordID + ")", null , null);
            }
            if (chkStartDate.Checked)
            {
                conditions.AddCondition("时间", ">=", dtpStartDate.Value.Date );
            }
            if (chkEndDate.Checked)
            {
                conditions.AddCondition("时间", "<", dtpEndDate.Value.Date.AddDays(1));
            }
            ORMBindingSource ds = MyApplicationFramework.Instance.ORMEngine.CreateBindingSource( typeof( Class费用信息 ),
                MyApplicationFramework.Instance.ORMEngine.ReadInstances(typeof(Class费用信息), conditions));
            myGridControl.DataSource = ds;

            ds = MyApplicationFramework.Instance.ORMEngine.CreateBindingSource(
                    typeof(Class人员基本信息),
                    MyApplicationFramework.Instance.ORMEngine.ReadAllInstances(typeof(Class人员基本信息)));
            ds.Insert(0, null);
            txtPeople.Properties.DataSource = ds;

            tvwPeoples.Nodes.Clear();
            foreach (Class工作单位 unit in MyApplicationFramework.Instance.ORMEngine.ReadAllInstances(typeof(Class工作单位)))
            {
                TreeNode unitNode = new TreeNode(unit.单位名称);
                tvwPeoples.Nodes.Add(unitNode);
                foreach (Class人员基本信息 people2 in unit.人员列表)
                {
                    TreeNode peopleNode = new TreeNode(people2.姓名);
                    peopleNode.Tag = people2;
                    unitNode.Nodes.Add(peopleNode);
                }
            }

        }

        private void tvwPeoples_AfterCheck(object sender, TreeViewEventArgs e)
        {

        }
    }
}